package com.example.management.config;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginHandlerInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("通过拦截器");
        Object username = request.getSession().getAttribute("account");
        if (username == null) {
            request.setAttribute("msg", "您还未登录，请先登录");
            return false;
        }
        return true;
    }

    /*
       //    下面是加了Token的preHandle
       @Override
       public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{

           //是不是映射到方法上
           boolean isHandlerMethod = handler instanceof HandlerMethod;
           if (!isHandlerMethod) {
               System.out.println("不是方法上");
               return true;
           }
           //需要登录验证
           String token = request.getHeader("Authorization");

           if (null == token) {
               System.out.println("无token");
           } else {
               Claims claims = null;
               try {
                   claims = com.parsley.utils.TokenUtil.parseJwt(token);
               } catch (Exception e) {
                   System.out.println("token有误");
               }
               if (null != claims) {
   //                Integer id = Integer.valueOf(claims.getId());
   //                String subject=claims.getSubject();
   //                if(subject.equals(LoginService.TYPE_ZU_KE)){
   //                    ZuKe zuKe = zuKeService.getById(id);
   //                    if(zuKe!=null){
   //                        request.setAttribute(LoginService.TYPE_ZU_KE,zuKe);
   //                        return true;
   //                    }
   //                }else if (subject.equals(LoginService.TYPE_FANG_DONG)){
   //                    FangDong fangDong =fangDongService.getById(id);
   //                    if (fangDong!=null){
   //                        request.setAttribute(LoginService.TYPE_FANG_DONG,fangDong);
   //                        return true;
   //                    }
   //                }
   //                return false;
               }
           }
           return false;
       }
   */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("控制器执行完毕");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("得到返回结果:" + response);
        System.out.println("请求完毕\n");
    }
}
